package com.zsc.shixun.controller;


import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.model.collect.CollectCreateVO;
import com.zsc.shixun.model.collect.CollectUpdateVO;
import com.zsc.shixun.service.ICollectCategoriesService;
import com.zsc.shixun.utils.LoginMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 收藏夹管理 前端控制器
 * </p>
 *
 * @author ZWYZY
 * @since 2020-06-06
 */
@Controller
@RequestMapping("/collectCategories")
@Validated
public class CollectCategoriesController {
    private static final Logger logger = LoggerFactory.getLogger(CollectCategoriesController.class);
    @Autowired
    private ICollectCategoriesService collectCategoriesService;
    @Autowired
    private LoginMessageUtils loginMessageUtils;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        request.setAttribute("articleVO", collectCategoriesService.getById(loginMessageUtils.getMessage().getLoginUser().getId()));
        return "admin/collect/index";
    }

    /**
     * 获取收藏夹类型列表
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @Secured(value = "ROLE_collect_select")
    public CommonResult getData() {
        return ResultUtil.success(collectCategoriesService.getData(loginMessageUtils.getMessage().getLoginUser().getId()));
    }

    /**
     * 修改收藏夹类型
     *
     * @param collectUpdateVO
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @Secured(value = "ROLE_collect_update")
    public CommonResult update(@Validated @ModelAttribute CollectUpdateVO collectUpdateVO) {
        collectCategoriesService.update(collectUpdateVO);
        return ResultUtil.success("修改收藏夹类型成功哦，亲");
    }

    /**
     * 删除收藏夹类型
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @Secured(value = "ROLE_collect_delete")
    public CommonResult delete(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        collectCategoriesService.delete(id, loginMessageUtils.getMessage().getLoginUser().getId());
        return ResultUtil.success("删除收藏夹类型成功哦，亲");
    }

    /**
     * 文章页/收藏管理----增加收藏夹类型
     *
     * @param collectCreateVO
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    @Secured(value = "ROLE_collect_insert")
    public CommonResult insert(@Validated @ModelAttribute CollectCreateVO collectCreateVO) {
        collectCategoriesService.insert(collectCreateVO, loginMessageUtils.getMessage().getLoginUser().getId());
        return ResultUtil.success("创建收藏夹类型成功哦，亲");
    }

    /**
     * 文章页-----点击收藏夹收藏文章
     *
     * @param aid
     * @param cid
     * @return
     */
    @GetMapping("/collect")
    @ResponseBody
    @Secured(value = "ROLE_collect_article")
    public CommonResult collectArticle(@NotNull(message = "文章id不能为空") @RequestParam(required = false) Long aid, @NotNull(message = "收藏夹id不能为空") @RequestParam(required = false) Long cid) {
        collectCategoriesService.collectArticle(aid, cid, loginMessageUtils.getMessage().getLoginUser().getId());
        return ResultUtil.success("收藏文章成功");
    }

}
